<% if @database == "pg"
     @db_url = "postgres://postgres:password@db:5432/#{database_name}_development"
     @wait_for = "while ! nc -q 1 db 5432 </dev/null; do sleep 1; done && "
   elsif @database == "mysql"
     @db_url = "mysql://admin:password@db:3306/#{database_name}_development"
     @wait_for = "while ! nc -q 1 db 3306 </dev/null; do sleep 1; done && "
   else
     @db_url = "sqlite3:./db/#{database_name}_development.db"
     @wait_for = ""
   end
-%>
version: '2'

services:
  app:
    build: .
    image: <%= @name %>
    command: amber watch
    environment:
      DATABASE_URL: <%= @db_url %>
    ports:
      - 3000:3000
    links:
      - db
    volumes:
    - .:/app
    - shards:/app/lib

  migrate:
    build: .
    image: <%= @name %>
    command: bash -c '<%= @wait_for %>amber db migrate seed'
    environment:
      DATABASE_URL: <%= @db_url %>
    volumes:
      - .:/app
    links:
      - db

<% if @database == "pg" -%>
  db:
    image: postgres
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
      POSTGRES_DB: <%= "#{database_name}_development" %>
    volumes:
      - db:/var/lib/postgres/data
<% elsif @database == "mysql" -%>
  db:
    image: mysql
    ports:
      - 3306:3306
    environment:
      MYSQL_USER: admin
      MYSQL_PASSWORD: password
      MYSQL_DATABASE: <%= "#{database_name}_development" %>
      MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
    volumes:
    - db:/var/lib/mysql
<% elsif @database == "sqlite" -%>
  db:
    image: <%= @name %>
    command: 'bash -c "ls > /dev/null"'
    volumes:
    - db:/app/local/db
<% end -%>

volumes:
  db:
  shards:
